//
//  ZGAddAndCutVC.m
//  扫码
//
//  Created by 鲍志刚 on 16/3/30.
//  Copyright © 2016年 鲍志刚. All rights reserved.
//

#import "ZGAddAndCutVC.h"
#import "ZGData.h"
#import "NetworkTools.h"
#import "UserData.h"


@interface ZGAddAndCutVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation ZGAddAndCutVC

-(NSMutableArray *)dataArray {
    
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
//    self.title = @"增删";
    
    
    
    [UserData datasWithURL:@"SweepServlet?method=showAll&&pageNum=1"  successBlock:^(NSArray *array) {
        
//       self.dataArray = array;
        for (id  temp in array) {
            [self.dataArray addObject:temp];
        }
        
        [self.tableView reloadData];
        
    } errorBlock:^(NSError *e) {
        NSLog(@"%@",e);
        
    }];
    
    
    [self setUpUI];
    
}


#pragma mark - 设置UI界面
- (void)setUpUI {

    

    [self setUpRefresh];
}


#pragma mark - 下拉刷新


- (void)setUpRefresh {
    //1.添加刷新控件
    UIRefreshControl *control=[[UIRefreshControl alloc]init];
    [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:control];
    
    //2.马上进入刷新状态，并不会触发UIControlEventValueChanged事件
    [control beginRefreshing];
    
    // 3.加载数据
    [self refreshStateChange:control];
}


#pragma mark - 加载数据
-(void)refreshStateChange:(UIRefreshControl *)control {
    
    [UserData datasWithURL:@"SweepServlet?method=showAll&&pageNum=1"  successBlock:^(NSArray *array) {
        
//        [self.dataArray addObject:array];
        
        for (id  temp in array) {
            [self.dataArray addObject:temp];
        }
//        self.dataArray = array;
        
        
        [self.tableView reloadData];
        
        [control endRefreshing];
        
    } errorBlock:^(NSError *e) {
        NSLog(@"%@",e);
        
        [control endRefreshing];

        
    }];

}

#pragma mark - 增加信息

- (IBAction)addInfo:(id)sender {
    
    NSLog(@"添加");
    
    [self setUpRefresh];
    
}



#pragma mark - 删除信息
- (IBAction)cutBtn:(id)sender {
    
    NSLog(@"刷新");
    
    
}


#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"cell";
    
    
    UserData *data = self.dataArray[indexPath.row];
    
    ZGData *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[ZGData alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    cell.userData = data;

    return cell;
    
}


#pragma mark -决定编辑模式

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        
        
        ZGData *cell = [[ZGData alloc] init];
        
        cell.userData = self.dataArray[indexPath.row];
        
        //    NSLog(@"%@", cell.userData.number);
        //[NSString stringWithFormat:@"number=%@",cell.userData.number]
        
        
        [[NetworkTools sharedTools] POST:@"SweepServlet?method=delete" parameters: @{@"number":[NSString stringWithFormat:@"%zd",cell.userData.number]} progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSLog(@"-----------------%@",responseObject);
            
            [self.tableView reloadData];
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@",error);
            
        }];
        
        // 1. 在数据源中把row 对应到 dataArray 下标中的对象 给移除掉
        [self.dataArray removeObjectAtIndex:indexPath.row];

        
        // 2. 刷新数据
   
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
    }


}


#pragma mark - 当cell被选中的时候调用

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"%ld",(long)indexPath.row);
    
    ZGData *cell = [[ZGData alloc] init];
    
    cell.userData = self.dataArray[indexPath.row];
    
//    NSLog(@"%@", cell.userData.number);
    //[NSString stringWithFormat:@"number=%@",cell.userData.number]
    
    
    [[NetworkTools sharedTools] POST:@"SweepServlet?method=delete" parameters: @{@"number":[NSString stringWithFormat:@"%zd",cell.userData.number]} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"-----------------%@",responseObject);
        
        [self.tableView reloadData];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
        
    }];
    
    
}

#pragma mark - 决定删除按钮上面显示的文本
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender {
    
    

}


@end
